package test01.common;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

public class MyApp {
	static Logger logger = Logger.getLogger("com.tsvetanv");
	static Logger barLogger = Logger.getLogger("com.tsvetanv.Bar");

	public static void main(String[] args) {
		// test1_Levels();
		// test2_NameLevel();
	}

	public static void test1_Levels() {
		// default configuration of the logging
		BasicConfigurator.configure();

		// Usually you don't need to set the level of a logger programmatically.
		// This is usually done in configuration files.
		logger.setLevel(Level.INFO);

		// -- TESTS ------------------------------------------------------------
		// ORDER: trace < debug < info < warn < error < fatal
		logger.info("INFO message of LOGGER"); // OK, info<= info
		logger.warn("WARN message of LOGGER"); // OK, info<= warn
		logger.error("ERROR message of LOGGER"); // OK, info<= error
		logger.fatal("FATAL message of LOGGER"); // OK, info<= fatal
		logger.debug("DEBUG message of LOGGER"); // FALSE, debug <= info
		logger.trace("TRACE message of LOGGER"); // FALSE, debug <= info
		logger.info("INFO-2 message of LOGGER" + "\n-----\n"); // OK, info<=
																// info

		// The logger instance barlogger, named "com.foo.Bar",
		// will inherit its level from the logger named "com.foo" .
		// Thus, the following request is enabled because WARN >= INFO.
		barLogger.warn("WARN message of BAR_LOGGER"); // OK, info<= warn
		barLogger.debug("DEBUG message of BAR_LOGGER"); // FALSE, debug <= info
		// This request is disabled.
		barLogger.info("INFO message of BAR_LOGGER"); // OK, info<= info
	}

	public static void test2NameLevel() {
		// test methods of log4j's Logger class
		System.out.println("LOGGER's default level: " + logger.getLevel()); // null
		System.out.println("RootLogger's default level: "
				+ logger.getRootLogger().getLevel()); // debug
		// logger.setLevel(Level.INFO);
		System.out.println("LOGGER - isTraceEnabled --> "
				+ logger.isTraceEnabled());

		System.out.println("LOGGER - isInfoEnabled --> "
				+ logger.isInfoEnabled());
		System.out.println("LOGGER - isDebugEnabled --> "
				+ logger.isDebugEnabled());

		System.out.println("LOGGER's name: " + logger.getName());
		System.out.println("LOGGER's parent name: "
				+ logger.getParent().getName());
		System.out.println("BAR_LOGGER's name:" + barLogger.getName());
		System.out.println("BAR_LOGGER's parent name:"
				+ barLogger.getParent().getName());
	}
}
